package com.bhs.sansong.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.bhs.sansong.base.Constants;
import com.bhs.sansong.ui.main.MainActivity;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class LocationUtils implements EasyPermissions.PermissionCallbacks{
    public String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE};
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    public static final int RC_LOCATION = 1;


    @AfterPermissionGranted(RC_LOCATION)
    public void methodRequiresTwoPermission(Activity mContext, AMapLocationListener listener) {
        if (EasyPermissions.hasPermissions(mContext, perms)) {
            if (mLocationClient == null) {
                //初始化定位
                try {
                    mLocationClient = new AMapLocationClient(mContext);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                //设置定位回调监听
                mLocationClient.setLocationListener(listener);
                //初始化AMapLocationClientOption对象
                mLocationOption = new AMapLocationClientOption();

                AMapLocationClientOption option = new AMapLocationClientOption();
                /**
                 * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
                 */
                option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
                if (null != mLocationClient) {
                    mLocationClient.setLocationOption(option);
                    //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
                    mLocationClient.stopLocation();
                    mLocationClient.startLocation();
                }

                //设置定位模式为AMapLocationMode.Battery_Saving，低功耗模式。
                mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
//            //获取一次定位结果：
//            //该方法默认为false。
                mLocationOption.setOnceLocation(true);
                //获取最近3s内精度最高的一次定位结果：
                //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
                mLocationOption.setOnceLocationLatest(true);
                //给定位客户端对象设置定位参数
                mLocationClient.setLocationOption(mLocationOption);
                //启动定位
                mLocationClient.startLocation();
            }
        } else {
            if (!SharedPreferencesUtils.getBoolean(mContext, "isFirst", false)) {
                SharedPreferencesUtils.saveBoolean(mContext, "isFirst", true);
                new XPopup.Builder(mContext)
                        .asConfirm("提示", "我需要您的位置信息来获得当前城市",
                                "取消", "确定",
                                new OnConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        // Do not have permissions, request them now
                                        EasyPermissions.requestPermissions(mContext, "我需要您的位置信息来获得当前城市",
                                                RC_LOCATION, perms);
                                    }
                                }, null, false)
                        .show();
            } else {
                EasyPermissions.requestPermissions(mContext, "我需要您的位置信息来获得当前城市",
                        RC_LOCATION, perms);
            }

        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        LogUtils.d(perms.size()+"gg");
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        LogUtils.d(perms.size()+"ppp");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        LogUtils.d(permissions.length+"????");
    }
}
